//
//  LeftViewController.m
//  MooMusic
//
//  Created by 守护神 on 15/10/19.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "LeftViewController.h"
// 导入MVviewcontroller
#import "AppDelegate.h"
#import "LTY_MVViewController.h"
#import "GCW_MineViewController.h"
#import "GCW_CollectionViewController.h"
// 导入地图的头文件
#import "ViewController.h"
@interface LeftViewController ()<UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>

@property (nonatomic, strong)UITableView *leftTableView;
@property (nonatomic, strong)UIImageView *backImageView;


@end

// 设置静态
static NSString *const cellInderfier = @"cellInderfier";

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backImageView = [[UIImageView alloc] init];
    self.backImageView.image = [UIImage imageNamed:@"lyric-sharing-background-08.png"];
    self.backImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.backImageView];
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.top.equalTo(self.view).with.offset(0);
        make.bottom.equalTo(self.view).with.offset(0);
    }];
    
    
    // 创建视图
    [self creatLeftTableView];
    
    // Do any additional setup after loading the view.
}

// 创建视图
- (void)creatLeftTableView
{
    self.leftTableView = [[UITableView alloc] init];
    self.leftTableView.backgroundColor = [UIColor lightGrayColor];
    self.leftTableView.scrollEnabled = NO;
    // 创建的时候使tableview的线显示
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.leftTableView.backgroundColor = [UIColor clearColor];
    [self.backImageView addSubview:self.leftTableView];
    
    [self.leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(0);
        make.top.equalTo(self.view).with.offset(50);
        make.bottom.equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(self.view.frame.size.width - 115);
    }];
    
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    
    // 注册
    [self.leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellInderfier];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInderfier];
    // 取消cell的背景颜色
    cell.backgroundColor = [UIColor clearColor];
    // 设置cell上的字体大小
    cell.textLabel.font = [UIFont systemFontOfSize:23];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"哞哞Music";
        cell.textLabel.font = [UIFont systemFontOfSize:28];
        cell.tag = 400;
        // cell的选中样式 (取消阴影效果)
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"哞哞MV";
        cell.tag = 401;
        cell.imageView.image = [UIImage imageNamed:@"iconfont-mv.png"];
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"哞哞走走";
        cell.tag = 406;
        cell.imageView.image = [UIImage imageNamed:@"iconfont-map.png"];
    }
    if (indexPath.row == 3) {
        cell.textLabel.text = @"收藏";
        cell.tag = 402;
        cell.imageView.image = [UIImage imageNamed:@"iconfont-collectshi.png"];
    }
    if (indexPath.row == 4) {
        cell.textLabel.text = @"夜间模式";
        cell.tag = 403;
        cell.imageView.image = [UIImage imageNamed:@"yejian.png"];
    }
    if (indexPath.row == 5) {
        cell.textLabel.text = @"清理缓存";
        cell.tag = 404;
        cell.imageView.image = [UIImage imageNamed:@"iconfont-shanchu.png"];
    }
    if (indexPath.row == 6) {
        cell.textLabel.text = @"关于";
        cell.tag = 405;
        cell.imageView.image = [UIImage imageNamed:@"iconfont-iconguanyu.png"];
    }
    
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消点击效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (indexPath.row == 1) {
        NSLog(@"点击进入页面  == %ld", indexPath.row);
        LTY_MVViewController *MVVC = [[LTY_MVViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:MVVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
    
    if (indexPath.row == 2) {
        NSLog(@"点击进入哞哞走走地图VC");
        ViewController *VC = [[ViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:VC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
        
        }
    
    if (indexPath.row == 3) {
            NSLog(@"点击进入收藏页面 == %ld", indexPath.row);
            GCW_CollectionViewController *CollectionVC = [[GCW_CollectionViewController alloc] init];
            UINavigationController *CollNaVC = [[UINavigationController alloc] initWithRootViewController:CollectionVC];
            [self presentViewController:CollNaVC animated:YES completion:^{
                
                
            }];

            
        }
        
    if (indexPath.row == 4) {
            NSLog(@"夜间模式");
        [NightMode shareNightMode].isNight = ![NightMode shareNightMode].isNight;
        [[NightMode shareNightMode] saveNightModeInfoToDisk];
        if ([NightMode shareNightMode].isNight) {
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"heiyejianglin" object:@"香皂" userInfo: dic];
            UITableViewCell *cell = (UITableViewCell *)[tableView viewWithTag:403];
            cell.textLabel.text = @"日间模式";
        }else{
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"limingjianglin" object:@"香皂" userInfo: dic];
            UITableViewCell *cell = (UITableViewCell *)[tableView viewWithTag:403];
            cell.textLabel.text = @"夜间模式";
        }

       
    
        }
    
    if (indexPath.row == 5) {
        NSLog(@"清理缓存");
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确认清除缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        
        }
    
    if (indexPath.row == 6) {
        NSLog(@"关于");
        GCW_MineViewController *MainVC = [[GCW_MineViewController alloc] init];
        UINavigationController *MainNaVC = [[UINavigationController alloc] initWithRootViewController:MainVC];
        [self presentViewController:MainNaVC animated:YES completion:^{
            
            
        }];

        
        
        }
        
    
        

    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (void)setNightDayModel {
    UITableViewCell *cell = (UITableViewCell *)[self.view viewWithTag:400];
    [NightManager setLabelColorWithLabel:cell.textLabel];
    UITableViewCell *cell1 = (UITableViewCell *)[self.view viewWithTag:401];
    [NightManager setLabelColorWithLabel:cell1.textLabel];
    UITableViewCell *cell2 = (UITableViewCell *)[self.view viewWithTag:402];
    [NightManager setLabelColorWithLabel:cell2.textLabel];
    UITableViewCell *cell3 = (UITableViewCell *)[self.view viewWithTag:403];
    [NightManager setLabelColorWithLabel:cell3.textLabel];
    UITableViewCell *cell4 = (UITableViewCell *)[self.view viewWithTag:404];
    [NightManager setLabelColorWithLabel:cell4.textLabel];
    UITableViewCell *cell5 = (UITableViewCell *)[self.view viewWithTag:405];
    [NightManager setLabelColorWithLabel:cell5.textLabel];
    UITableViewCell *cell6 = (UITableViewCell *)[self.view viewWithTag:406];
    [NightManager setLabelColorWithLabel:cell6.textLabel];

}

- (void)setLightDayModel {
    UITableViewCell *cell = (UITableViewCell *)[self.view viewWithTag:400];
    [NightManager setLabelColorWithLabel:cell.textLabel];
    UITableViewCell *cell1 = (UITableViewCell *)[self.view viewWithTag:401];
    [NightManager setLabelColorWithLabel:cell1.textLabel];
    UITableViewCell *cell2 = (UITableViewCell *)[self.view viewWithTag:402];
    [NightManager setLabelColorWithLabel:cell2.textLabel];
    UITableViewCell *cell3 = (UITableViewCell *)[self.view viewWithTag:403];
    [NightManager setLabelColorWithLabel:cell3.textLabel];
    UITableViewCell *cell4 = (UITableViewCell *)[self.view viewWithTag:404];
    [NightManager setLabelColorWithLabel:cell4.textLabel];
    UITableViewCell *cell5 = (UITableViewCell *)[self.view viewWithTag:405];
    [NightManager setLabelColorWithLabel:cell5.textLabel];
    UITableViewCell *cell6 = (UITableViewCell *)[self.view viewWithTag:406];
    [NightManager setLabelColorWithLabel:cell6.textLabel];
    
}

#pragma mark -- 清理缓存
// alterView 当点击不同的按钮时实现不通的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"不清除");
    }
    if (buttonIndex == 1) {
        //NSLog(@"清除数据库");
        //***************************清除缓存("NSCachesDirectory" 清除的地址路径, (ˇˍˇ） 想～清除哪个缓存就替换路径, 一般都是NSCachesDirectory)
        dispatch_async(
                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                       , ^{
                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                           
                           NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                           NSLog(@"files :%lu",[files count]);
                           for (NSString *p in files) {
                               NSError *error;
                               NSString *path = [cachPath stringByAppendingPathComponent:p];
                               if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                   [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                               }
                           }
                           [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
        
    }
}

-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
